import com.alibaba.fastjson.JSON;
import com.fy56.platform.goodsTracking.commons.DateUtil;
import com.fy56.platform.goodsTracking.commons.security.MD5;
import com.fy56.platform.goodsTracking.web.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.ksoap2.serialization.SoapObject;
import org.xml.sax.InputSource;

import java.io.StringReader;
import java.util.List;

/**
 * Created by XTK on 2016-11-24.
 */
public class text {

    public static void main(String[] args) {


     CargoService cargoService = new CargoService();
        UserClientRequest clientRequest = new UserClientRequest();
        clientRequest.setUserName("19601");
        clientRequest.setUserPassWord(MD5.digest("123456").toLowerCase());
        UserToken  userToken = cargoService.getCargoServiceSoap12().userAuthenticate(
                clientRequest);

        String result = JSON.toJSONString(userToken);
        System.out.println(result);
/**/
/*
        UserClientRequest ucr = new UserClientRequest();
        ucr.setUserName("7169");
        ucr.setUserPassWord("e10adc3949ba59abbe56e057f20f883e");

        CargoService cargoService = new CargoService();
        UserToken userToken = cargoService.getCargoServiceSoap12()
                .userAuthenticate(ucr);

        String result = JSON.toJSONString(userToken);

        System.out.println(result);
*/

    /*    String sCargoXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<Root>\n" +
                "  <Header>\n" +
                "    <MessageReferenceNumber>1c691654-a26e-4240-9b99-14f4f7fd9b90</MessageReferenceNumber>\n" +
                "    <DocumentName>普通运输跟踪托运单</DocumentName>\n" +
                "    <DocumentVersionNumber>V3.1.2</DocumentVersionNumber>\n" +
                "    <SenderCode>7169</SenderCode>\n" +
                "    <MessageSendingDateTime>20160907111233</MessageSendingDateTime>\n" +
                "    <MessageFunctionCode>02</MessageFunctionCode>\n" +
                "  </Header>\n" +
                "  <Body>\n" +
                "    <ShippingNoteNumber>HHR16082</ShippingNoteNumber>\n" +
                "    <OriginalDocumentNumber/>\n" +
                "    <GoodsSelfNumber>AC160829-1-3</GoodsSelfNumber>\n" +
                "    <InHouseIdentifier>HY_2016</InHouseIdentifier>\n" +
                "    <ConsignmentDateTime>20160907111233</ConsignmentDateTime>\n" +
                "    <ConsignorInfo>\n" +
                "      <ConsignorCode/>\n" +
                "      <ContactName>阿凡提</ContactName>\n" +
                "      <TelephoneNumber>15612340000</TelephoneNumber>\n" +
                " <CountrySubdivisionName>保定</CountrySubdivisionName>\n" +
                " <CountrySubdivisionCode>1306</CountrySubdivisionCode>\n" +
                "      <PlaceOfLoading>哈尔滨</PlaceOfLoading>\n" +
                "      <ResquestedLoadingDateTime>20160907111233</ResquestedLoadingDateTime>\n" +
                "    </ConsignorInfo>\n" +
                "    <ConsigneeInfo>\n" +
                "      <ConsigneeCode/>\n" +
                "      <ContactName>京东A</ContactName>\n" +
                "      <TelephoneNumber>15011220033</TelephoneNumber>\n" +
                "      <CountrySubdivisionName>沈阳</CountrySubdivisionName>\n" +
                "      <CountrySubdivisionCode>2101</CountrySubdivisionCode>\n" +
                "      <GoodsReceiptPlace>长春</GoodsReceiptPlace>\n" +
                "    </ConsigneeInfo>\n" +
                "    <Remark/>\n" +
                "    <GoodsInfo>\n" +
                "      <DescriptionOfGoods>服装</DescriptionOfGoods>\n" +
                "      <TotalNumberOfPackages>3</TotalNumberOfPackages>\n" +
                "      <PackageTypeCode/>\n" +
                "    </GoodsInfo>\n" +
                "  </Body>\n" +
                "</Root>\n";
*//*        String xml = "";
        try {
            xml = new String(sCargoXML.getBytes("UTF-8"), "GB2312");
        } catch (Exception e) {
            e.printStackTrace();
        }*//*
        UserToken userToken1 = new UserToken();
        userToken1.setUserName("7169");
        userToken1.setUserPassWord(MD5.digest("123456").toUpperCase());
        userToken1.setTokenID("2E8B8C24BCB162A1AD31E0C3D612C5B5");
//UpdateCargoTrack //updateCargoTrackList
        CargoService cargoService1 = new CargoService();
        ResultObject ro = cargoService1.getCargoServiceSoap12()
                .updateCargoTrack(userToken1, sCargoXML);
        String result1 = "";
        if (ro != null && ro.isResultType()) {
            result1 = xmlElements(ro.getResultXML());
        } else {
            result1 = ro.getCodeNum() + "," + ro.getCodeMessage();
        }
        System.out.print(result1);
*/
    /*    String sKey = "";
        sKey = "e26785fd7a6497c79a976195e6eaaa7a";// "fy56.com"

        String sMethod = "UserAuthenticate";
        SoapObject soapobject = new SoapObject(url, sMethod);

        Tenant t=new Tenant();
        t.setUserName("7169");
        t.setUserPassWord("123456");
        soapobject.addProperty("tenant", t);
        //soapobject.addProperty("UserPassWord", password);
       *//* soapobject.addProperty("TokenID", tenant);*//*
   *//*     soapobject.addProperty("TokenID", tenant);*//*
        try {
            String dd = WebServiceUtil.DoService_Reslut(ns, url, sMethod, soapobject);
            System.out.print(dd);
        } catch (Exception e) {

        }*/

    }

    public static String xmlElements(String xmlDoc) {
        String result = "no";
        // 创建一个新的字符串
        StringReader read = new StringReader(xmlDoc);
        // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
        InputSource source = new InputSource(read);
        // 创建一个新的SAXBuilder
        SAXBuilder sb = new SAXBuilder();
        try {
            // 通过输入源构造一个Document
            Document doc = sb.build(source);
            // 取的根元素
            Element root = doc.getRootElement();
            // 得到根元素所有子元素的集合
            List jiedian = root.getChildren();
            Element et = (Element) jiedian.get(0);
            List zjiedian = et.getChildren();
            for (int j = 0; j < zjiedian.size(); j++) {
                Element xet = (Element) zjiedian.get(j);
                if (xet.getName().equals("Info"))
                    continue;
                result += xet.getName() + "#:#" + xet.getText() + "#,#";
            }
            result += "#;#";
            Element et1 = (Element) et.getChildren("Info").get(0);
            List info = et1.getChildren();
            for (int i = 0; i < info.size(); i++) {
                Element iet = (Element) info.get(i);
                result += iet.getName() + "#:#" + iet.getText() + "#,#";
            }
            result += "#;#";
            Element et2 = (Element) et.getChildren("Info").get(1);
            List info1 = et2.getChildren();
            for (int i = 0; i < info1.size(); i++) {
                Element iet = (Element) info1.get(i);
                result += iet.getName() + "#:#" + iet.getText() + "#,#";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
